﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using FPC.Offer.Common;
using System.IO;
namespace FPC.Offer.UI.GeneralProcessor.MangerOrganize
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class OrginzeManger : IHttpHandler
    {
        FPC.Offer.Service.Police_FuzzTableService fuzz = new Service.Police_FuzzTableService();
        FPC.Offer.Service.System_DeptTableService dept = new Service.System_DeptTableService();
        FPC.Offer.Service.Police_OrganizeTalbeService organize = new Service.Police_OrganizeTalbeService();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var Actioon = context.Request["Actioon"].ToString();
            var name="";
            var deptid = 0;
            if (Actioon == EnumAction.Select.ToString())
            {
                if (context.Request["name"] != "" && context.Request["name"] != null)
                {
                    name = context.Request["name"].ToString();
                }
                if (Convert.ToInt32(context.Request["id"]) != 0)
                {
                    deptid = (Convert.ToInt32(context.Request["id"]));
                }

                var fuzzlist = fuzz.Select();
                var deptlist = dept.Select();
                var organizelist = organize.Select();

                var list = (from b in fuzzlist
                            join p in deptlist
                               on b.AlarmId equals p.AlarmId
                               where b.RoleId==3
                               select new
                               {
                                   id = p.DeptId,
                                   alarmid=p.AlarmId,
                                   oid=p.OrganizeId,
                                   deptname = p.OreganizeSublevelId,
                                   user=b.UserName,
                                   tel=b.UserTel,
                                   email=b.UserEmail,
                                   addr=b.UserAddress

                               }).ToList();

                                var newlist=(from b in list join p in organizelist on b.oid equals p.OrganizeId  
                            select new {
                                   id = b.id,
                                   alarmid=b.alarmid,
                                   oid=p.OrganizeId,
                                   oname=p.OrganizeName,
                                   deptname = b.deptname,
                                   user=b.user,
                                   tel=b.tel,
                                   email=b.email,
                                   addr=b.addr
                                    }).ToList();

                                var list1 = (from b in fuzzlist
                                             join p in deptlist
                                             on b.AlarmId equals p.AlarmId
                                             where b.RoleId != 3
                                             select new
                                             {
                                                 id = p.DeptId,
                                                 alarmid = p.AlarmId,
                                                 oid = p.OrganizeId,
                                                 deptname = p.OreganizeSublevelId,
                                                 user = "待添加",
                                                 tel = "待添加",
                                                 email = "待添加",
                                                 addr = "待添加",
                                             }).ToList();
                                var newlist1 = (from b in list1
                                               join p in organizelist on b.oid equals p.OrganizeId
                                               select new
                                               {
                                                   id = b.id,
                                                   alarmid = b.alarmid,
                                                   oid = p.OrganizeId,
                                                   oname = p.OrganizeName,
                                                   deptname = b.deptname,
                                                   user = "待添加",
                                                   tel = "待添加",
                                                   email = "待添加",
                                                   addr = "待添加"
                                               }).ToList();

                 newlist.AddRange(newlist1);
                if (deptid != 0 || (name != "" && name != null))
                {
                    if (name != "" && name != null && deptid == 0)
                    {
                       var lst = newlist.Where(p => p.deptname.Contains(name)).ToList();
                        context.Response.Write(JsonConvert.SerializeObject(lst));
                    }
                    if (deptid != 0 && (name == "" || name == null))
                    {
                        var emp = newlist.Where(p => p.id == deptid).FirstOrDefault();
                        context.Response.Write(JsonConvert.SerializeObject(emp));
                    }
                }
                else
                {
                    context.Response.Write(JsonConvert.SerializeObject(newlist));
                }

            }



            //添加
            if (Actioon == EnumAction.Add.ToString())
            {
                Model.Police_DeptTable dep = new Model.Police_DeptTable();
                dep.OreganizeSublevelId = context.Request["getdeptname"].ToString();
                dep.OrganizeId = Convert.ToInt32(context.Request["getid"]);
                dep.AlarmId = Convert.ToInt32(context.Request["getmanager"]);
               
                int i = dept.Add(dep);
                context.Response.Write(i);
            }
            //修改
            if (Actioon == EnumAction.Update.ToString())
            {                
                if (context.Request["getmanager"] != null && context.Request["getdeptname"] != null && context.Request["getid"] != null)
                {
                    Model.Police_DeptTable emp = new Model.Police_DeptTable();
                    emp.AlarmId = Convert.ToInt32(context.Request["getmanager"].ToString());
                    emp.OreganizeSublevelId = context.Request["getdeptname"].ToString();
                    emp.OrganizeId = Convert.ToInt32(context.Request["getid"].ToString());
                    emp.DeptId =Convert.ToInt32(context.Request["getid"]);
                    context.Response.Write( dept.Update(emp));
                }
               
            }
            if (Actioon == EnumAction.Delete.ToString())
            {
                    int id = Convert.ToInt32(context.Request["id"]);
                    var i = dept.Delete(dept.Find(x => x.DeptId == id).FirstOrDefault());
                    context.Response.Write(i);               
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}